<div class="col-md-8">
<div class="main-content" id="main-content">
	<div class="sub-content">
		<div class="top-header products clearfix">
			<div class="pull-left pad">
				<h1>Product List</h1>				
			</div>
			<div class="product-list-details pull-right">
				<a href="<? echo $this->Url('products')?>/add-product?is_first=1"><input type="button" value="Add product" class="btn"/></a>
			</div>
			
		</div>
		<div class="col-md-12">
			<form id="forms" method="post" class="form-horizontal" role="form">
			<div class="form-group">
			<label class="control-label col-sm-3">Country:</label>
			<div class="col-sm-9">
			<? if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']!='user') {?>
				<select id="Country" name="Country" onchange="changeCategory('country')">	
					<option value="all">--Select Country--</option>		
					<? if(count($this->getallcountries)>0){foreach($this->getallcountries as $getallcountries){?>						
						<? if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='superadmin'){?>
							<option value="<? echo $getallcountries->country_id;?>"><? echo $getallcountries->nicename;?></option>
						<?}?>
					<?}}?>
				</select>
			<?}else{?>
				<select id="Country" name="Country" onchange="changeCategory('country')">	
					<option value="all">--Select Country--</option>
					<? if(isset($this->getallcountries) && $this->getallcountries!=""){?>
					<option value="<? echo $this->getallcountries;?>"><? echo $_SESSION['user']['countryname'];?></option>
					<?}?>					
				</select>
			<?}?></div></div><div class="form-group">
			
				<label class="control-label col-sm-3">Industry:</label>
				<div class="col-sm-9">
				<select id="Industry" name="Industry" onchange="changeCategory('industry')">
					<option value="">--Select Industry--</option>
					<? if(count($this->industries)>0){foreach($this->industries as $industries){?>
						<option value="<? echo $industries->industry_id;?>"><? echo $industries->industryname;?></option>
					<?}}?>
				</select>
				</div></div>
				<div class="form-group">
				<label class="control-label col-sm-3">Categories:</label>
				<div class="col-sm-9">
				<select id="category" name="category" onchange="changeCategory('category')">
					<option value="">--Select category--</option>
					<? if(count($this->categories)!=0){ foreach($this->categories as $catogories){?>
						<option value="<? echo $catogories->category_id;?>"><? echo $catogories->category_name;?></option>
					<?}}?>
				</select></div>
		</div>
		
		
		</form>
		<div class="product_table table-responsive">			
		</div>
	</div></div>
</div> </div>
<style>.table td{word-break:break-all;}</style>
<script>
function changeCountry(){
	var country_id=$("#Country").val();
	if(country_id!='all'){
		$.ajax({
			type:	'POST',
			url:   	BASE_PATH+'/products/get-industries',
			data:	{country_id:country_id},
			success: function(data){
				if(data.output=='success'){
					$("#Industry").html(data.industries);
				}
			}
		});
	}else{
		changeCategory('all');
	}
}
function changeIndustry(industry_id){
	var country_id=$("#Country").val();
	$.ajax({
		type:	'POST',
		url:   	BASE_PATH+'/products/get-categories',
		data:	{country_id:country_id,industry_id:industry_id,check:'check'},
		success: function(data){
			if(data.output=='success'){
				$("#category").html(data.categories);
			}
		}
	});
}
$(function(){
	changeCategory('all');
});
function changeCategory(type){
$('.product_table').html('Please wait loading....');
   if(type=='all'){					
		var cid='all';		
		var inid='all';	
		var catid='all';
		$.ajax({
			type:	'POST',
			dataType:'json',
			url:   	BASE_PATH+'/products/product-ajax',
			data:	{country_id:cid,industry_id:inid,category_id:catid},
			success: function(data){
			$('.product_table').html('<table class="table table-bordered" cellpadding="0" cellspacing="0" width="100%" id="search_results"><thead><tr class="bg_header"><th>SNO</th><th>Generic Name</th><th>Manufacturer&nbsp;</th><th>Quantity</th><th>Unites</th><th>Type</th><th>Status</th><th>Action</th></tr></thead><tbody></tbody></table>');	
				if(data!='1'){
					eval(data);
					var oTable = $('#search_results').dataTable( {
							aoColumnDefs: [{bSortable: false,
											aTargets: [ 0 ]
											}],
							"bStateSave": true,
							"bAutoWidth": true,
							"sRowSelect": "multi",
							"sPaginationType": "full_numbers",
							"bProcessing": true,
							"aaData": data,
							"aoColumns": [
								{ "mData": "sno" },
								{ "mData": "genericname" },
								{ "mData": "manufacturer" },
								{ "mData": "quntity" },
								{ "mData": "unites" },
								{ "mData": "saleing" },
								{ "mData": "status" },
								{ "mData": "action" }
							] 
						});			
				}else{
					$('.product_table').html('No Products...');	
				}
			}
		});
	}else if(type=='country'){
		var cid=$("#Country").val();
		var inid='all';		
		$.ajax({
			type:	'POST',
			url:   	BASE_PATH+'/products/get-industries',
			data:	{country_id:cid,industry_id:inid,check:'check'},
			success: function(data){
				if(data.output=='success'){				
					$("#Industry").html(data.industries);
					changeIndustry(inid);
					var catid='all';
					$.ajax({
						type:	'POST',
						dataType:'json',
						url:   	BASE_PATH+'/products/product-ajax',
						data:	{country_id:cid,industry_id:inid,category_id:catid},
						success: function(data){
						$('.product_table').html('<table class="table table-bordered" cellpadding="0" cellspacing="0" width="100%" id="search_results"><thead><tr class="bg_header"><td>SNO</td><td>Generic Name</td><td width="77px;">Manufacturer&nbsp;</td><td>Quantity</td><td>Unites</td><td>Type</td><td>Status</td><td>Action</td></tr></thead><tbody></tbody></table>');	
							if(data!='1'){
								eval(data);
								var oTable = $('#search_results').dataTable( {
										aoColumnDefs: [{bSortable: false,
														aTargets: [ 0 ]
														}],
										"bStateSave": true,
										"bAutoWidth": true,
										"sRowSelect": "multi",
										"sPaginationType": "full_numbers",
										"bProcessing": true,
										"aaData": data,
										"aoColumns": [
											{ "mData": "sno" },
											{ "mData": "genericname" },
											{ "mData": "manufacturer" },
											{ "mData": "quntity" },
											{ "mData": "unites" },
											{ "mData": "saleing" },
											{ "mData": "status" },
											{ "mData": "action" }
										] 
									});			
							}else{
								$('.product_table').html('No Products...');	
							}
						}
					});
				}
			}
		});
	}else if(type=='industry'){
		var cid=$("#Country").val();
		var inid=$("#Industry").val();
		var check='check';		
		$.ajax({
			type:	'POST',
			url:   	BASE_PATH+'/products/get-categories',
			data:	{country_id:cid,industry_id:inid,check:'check'},
			success: function(data){
				if(data.output=='success'){
					$("#category").html(data.categories);
					var catid=$("#category").val();
					$.ajax({
						type:	'POST',
						dataType:'json',
						url:   	BASE_PATH+'/products/product-ajax',
						data:	{country_id:cid,industry_id:inid,category_id:catid},
						success: function(data){
						$('.product_table').html('<table class="table table-bordered" cellpadding="0" cellspacing="0" width="100%" id="search_results"><thead><tr class="bg_header"><td>SNO</td><td>Generic Name</td><td width="77px;">Manufacturer&nbsp;</td><td>Quantity</td><td>Type</td><td>Unites</td><td>Status</td><td>Action</td></tr></thead><tbody></tbody></table>');	
							if(data!='1'){
								eval(data);
								var oTable = $('#search_results').dataTable( {
										aoColumnDefs: [{bSortable: false,
														aTargets: [ 0 ]
														}],
										"bStateSave": true,
										"bAutoWidth": true,
										"sRowSelect": "multi",
										"sPaginationType": "full_numbers",
										"bProcessing": true,
										"aaData": data,
										"aoColumns": [
											{ "mData": "sno" },
											{ "mData": "genericname" },
											{ "mData": "manufacturer" },
											{ "mData": "quntity" },
											{ "mData": "unites" },
											{ "mData": "saleing" },
											{ "mData": "status" },
											{ "mData": "action" }
										] 
									});			
							}else{
								$('.product_table').html('No Products...');	
							}
						}
					});
				}
			}
		});
	}else if(type=='category'){
		var cid=$("#Country").val();
		var inid=$("#Industry").val();
		var catid=$("#category").val();	
		$.ajax({
			type:	'POST',
			dataType:'json',
			url:   	BASE_PATH+'/products/product-ajax',
			data:	{country_id:cid,industry_id:inid,category_id:catid},
			success: function(data){
			$('.product_table').html('<table class="table table-bordered" cellpadding="0" cellspacing="0" width="100%" id="search_results"><thead><tr class="bg_header"><td>SNO</td><td>Generic Name</td><td width="77px;">Manufacturer&nbsp;</td><td>Quantity</td><td>Unites</td><td>Type</td><td>Status</td><td>Action</td></tr></thead><tbody></tbody></table>');	
				if(data!='1'){
					eval(data);
					var oTable = $('#search_results').dataTable( {
							aoColumnDefs: [{bSortable: false,
											aTargets: [ 0 ]
											}],
							"bStateSave": true,
							"bAutoWidth": true,
							"sRowSelect": "multi",
							"sPaginationType": "full_numbers",
							"bProcessing": true,
							"aaData": data,
							"aoColumns": [
								{ "mData": "sno" },
								{ "mData": "genericname" },
								{ "mData": "manufacturer" },
								{ "mData": "quntity" },
								{ "mData": "unites" },
								{ "mData": "saleing" },
								{ "mData": "status" },
								{ "mData": "action" }
							] 
						});			
				}else{
					$('.product_table').html('No Products...');	
				}
			}
		});
	}
}
function deleteProduct(pro_id){
	if(confirm("Are you sure you want delete record?")){
		window.location=BASE_URL+"/products/delete-products?product_id="+pro_id;					
	}
}
</script>